import 'package:flutter/material.dart';
import 'dart:convert';
import '../util/DataUtils.dart';
import '../util/ToastUtils.dart';
import '../api/Api.dart';
import '../pages/musicList.dart';

class LoginPage extends StatefulWidget {
    
    @override
    State<StatefulWidget> createState() {
        return new _LoginPageState();
    }
}

class _LoginPageState extends State<LoginPage> {
    var leftRightPadding = 30.0;
    var topBottomPadding = 4.0;
    var textTips = new TextStyle(fontSize: 16.0, color: Colors.black);
    var hintTips = new TextStyle(fontSize: 15.0, color: Colors.black26);
    static const LOGO = "images/dance_logo_graphic.png";
    
    var _userPassController = new TextEditingController();
    var _userNameController = new TextEditingController();

    void _todoLogin(String strUser,String strPass) {
        print('username:$strUser,passwd:$strPass, todo login...');
        
        if(strUser.isEmpty) {
            ToastUtils.showMsg('请输入用户名');
            return;
        }

        if(strPass.isEmpty) {
            ToastUtils.showMsg('请输入密码');
            return;
        }
        
        if(strUser.isNotEmpty && strPass.isNotEmpty) {
            Map<String, String> _param = new Map();
            _param['phone'] = strUser;
            _param['passwd'] = strPass;
            DataUtils.netPost(Api.LOGIN_API, params: _param).then((res) {
                print(res);
                if (res != null) {
                    var obj = json.decode(res);
                    if (obj['code'] == 1) {
                        ToastUtils.showMsg('登录成功');
                        DataUtils.saveLoginInfo(obj['data']);
                        Navigator.of(context).pushAndRemoveUntil(
                            new MaterialPageRoute(builder: (context) => new MusicList()
                            ), (route) => route == null);
                    } else {
                        ToastUtils.showAlertDialog(context, obj['msg']);
                    }
                }
            }).catchError((e) {
                print('网络请求出错:$e');
                ToastUtils.showAlertDialog(context, '网络请求出错');
            });
        } else {
            print('请填写账号和密码');
            ToastUtils.showAlertDialog(context, '请填写账号和密码');
        }
    }
    
    @override
    Widget build(BuildContext context) {
        return new Scaffold(
            appBar: new AppBar(
                title: new Text("奇妙时代", style: new TextStyle(color: Colors.white)),
            ),
            body: new SingleChildScrollView(
                child: new ConstrainedBox(
                    constraints: new BoxConstraints(
                        minHeight: 120.0,
                    ),
                    child: new Column(
                        mainAxisSize: MainAxisSize.max,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                            new Padding(
                                padding: new EdgeInsets.fromLTRB(
                                    leftRightPadding, 50.0, leftRightPadding, 10.0),
                                child: new Image.asset(LOGO),
                            ),
                            new Padding(
                                padding: new EdgeInsets.fromLTRB(
                                    leftRightPadding, 50.0, leftRightPadding, topBottomPadding),
                                child: new TextField(
                                    style: hintTips,
                                    controller: _userNameController,
                                    decoration: new InputDecoration(
                                        hintText: "请输入用户名",
                                        prefixIcon: Icon(Icons.person)
                                    ),
                                    //obscureText: true,
                                ),
                            ),
                            new Padding(
                                padding: new EdgeInsets.fromLTRB(
                                    leftRightPadding, 30.0, leftRightPadding, topBottomPadding),
                                child: new TextField(
                                    style: hintTips,
                                    controller: _userPassController,
                                    decoration: new InputDecoration(
                                        hintText: "请输入密码",
                                        prefixIcon: Icon(Icons.lock)
                                    ),
                                    obscureText: true,
                                ),
                            ),
                            new Container(
                                width: 360.0,
                                margin: new EdgeInsets.fromLTRB(10.0, 40.0, 10.0, 0.0),
                                padding: new EdgeInsets.fromLTRB(leftRightPadding,
                                    topBottomPadding, leftRightPadding, topBottomPadding),
                                child: new Card(
                                    color: Colors.blue,
                                    elevation: 6.0,
                                    child: new FlatButton(
                                        onPressed: () {
                                            //print("the user is" + _userNameController.text + "," +  _userPassController.text);
                                            _todoLogin(_userNameController.text, _userPassController.text);
                                        },
                                        child: new Padding(
                                            padding: new EdgeInsets.all(10.0),
                                            child: new Text(
                                                '马上登录',
                                                style:
                                                new TextStyle(color: Colors.white, fontSize: 16.0),
                                            ),
                                        )),
                                ),
                            )
                        ],
                    ),
                ),
            ),
        );
        
    }
}
